#include "lrcpage.h"
#include "ui_lrcpage.h"
#include <QPushButton>
#include <QFile>
#include <QDebug>

LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);

    //设置窗口无标题栏
    setWindowFlag(Qt::FramelessWindowHint);

    //下拉按钮处理
    connect(ui->hideBtn, &QPushButton::clicked, this, [=](){
        animation->start();
    });

    //初始化歌词下降动画
    animation = new QPropertyAnimation(this, "geometry", this);
    animation->setDuration(500);
    animation->setStartValue(QRect(10, 10, width(), height()));
    animation->setEndValue(QRect(10, 10+height(), width(), height()));

    //动画完成后隐藏页面
    connect(animation, &QPropertyAnimation::finished, this, [=](){
        this->hide();
    });

    //设置下拉按钮图标
    ui->hideBtn->setIcon(QIcon(":/images/xiala.png"));
}

LrcPage::~LrcPage()
{
    delete ui;
}

void LrcPage::parseLrcFile(const QString &path)
{
    //打开文件
    QFile file(path);
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug() << path <<" 文件打开失败";
    }

    //清空上一首歌的lrc
    lrcWordLines.clear();

    while(!file.atEnd())
    {
        QString line = file.readLine(1024);

        //读文件
        //[分:秒.毫秒]歌词
        //[分:秒.毫秒.00]歌词

        //解析出时间和歌词
        int start = 0, end = 0;
        end = line.indexOf(']', start);
        QString lrcTime = line.mid(start, end - start + 1);
        QString lrcWord = line.mid(end + 1, line.size() - end - 1 - 1);

        //将时间转化为毫秒
        qint64 lineTime = 0;
        //解析分
        start = 1;
        end = line.indexOf(':', start);
        lineTime += line.mid(start, end - start).toInt()*60*1000;

        //解析秒
        start = end + 1;
        end = line.indexOf('.', start);
        lineTime += line.mid(start, end - start).toInt()*1000;

        //解析毫秒
        start = end + 1;
        end = line.indexOf('.', start);
        lineTime += line.mid(start, end - start).toInt();

        lrcWordLines.push_back(qMakePair<qint64, QString>(lineTime, lrcWord));
    }

    for(auto e:lrcWordLines)
    {
        qDebug() << e.first << " : " << e.second;
    }
}

int LrcPage::getLrcWordLineIndex(qint64 time)
{
    if(lrcWordLines.isEmpty())
    {
        return -1;
    }

    //进度小于第一句歌词时间,显示第一句
    if(time < lrcWordLines[0].first)
    {
        return 0;
    }

    for(int i = 1; i < lrcWordLines.size(); i++)
    {
        //当前歌词时间到下一句开始显示当前句
        if(time >= lrcWordLines[i-1].first && time < lrcWordLines[i].first)
        {
            return i-1;
        }
    }

    //最后一句歌词时间到音乐结束都显示最后一句
    return lrcWordLines.size()-1;
}

QString LrcPage::getLrcWordLineByIndex(int index)
{
    if(index < 0 || index >= lrcWordLines.size())
    {
        return "";
    }

    return lrcWordLines[index].second;
}

void LrcPage::showLrcWordLine(qint64 time)
{
    int index = getLrcWordLineIndex(time);

    if(index == -1)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("当前歌曲暂无歌词");
        ui->line5->setText("");
        ui->line6->setText("");
        ui->line7->setText("");
    }
    else
    {
        ui->line1->setText(getLrcWordLineByIndex(index-3));
        ui->line2->setText(getLrcWordLineByIndex(index-2));
        ui->line3->setText(getLrcWordLineByIndex(index-1));
        ui->lineCenter->setText(getLrcWordLineByIndex(index));
        ui->line5->setText(getLrcWordLineByIndex(index+1));
        ui->line6->setText(getLrcWordLineByIndex(index+2));
        ui->line7->setText(getLrcWordLineByIndex(index+3));
    }
}
